package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.service.ILulianIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 首页控制器
 * @author candie
 */
@Api(tags = "首页控制器")
@RestController
@RequestMapping("index")
public class IndexController extends WebController {

    private ILulianIndexService lulianIndexService;

    public IndexController( ILulianIndexService lulianIndexService) {
        this.lulianIndexService = lulianIndexService;
    }

    /**
     * 获取当前登陆人的企业名称
     * @return 结果
     */
    @GetMapping("getIndexCompanyName/{phone}")
    public Message getIndexCompanyName(@PathVariable("phone") String phone){
        String userId = findUserId();
        String companyName = lulianIndexService.getIndexCompanyName(userId);
        if(StringUtils.isBlank(companyName)){
            companyName = phone;
        }
        return Message.success(companyName);
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-09-07
     * @Description: 获取当前登录人的企业相关信息
     */
    @ApiOperation("获取当前用户的企业相关信息")
    @GetMapping("/getCompanyInformation")
    public Message getCompanyInformation(String userId){
        try {
            if (StrUtil.isNotBlank(userId)) {
                return Success(lulianIndexService.getCompanyInformation(userId));
            } else {
                logger.error("【getCompanyInformation】方法查询异常：===》入参异常");
                return Fail("入参异常！");
            }
        } catch (Exception e) {
            logger.error("【getCompanyInformation】方法查询异常：===》" + e);
            return Fail(e.getMessage());
        }
    }

    @ApiModelProperty("在认证企业表中查询用户绑定的企业信息")
    @GetMapping("/getHistoryCompanyInformation")
    public Message getHistoryCompanyInformation(@RequestParam("userId") String userId) {
        try {
            return Success(lulianIndexService.getCeCompanyInformation(userId));
        }catch (Exception e) {
            logger.error("【getHistoryCompanyInformation】方法查询异常：===》" + e);
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常：" + e.getMessage());
        }
    }

}

